package demoxsgl_300.com.gouwu1500.zhi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demoxsgl_300.com.gouwu1500.R;
import demoxsgl_300.com.gouwu1500.bean.BEA;
import demoxsgl_300.com.gouwu1500.bean.ShiLiao;
import demoxsgl_300.com.gouwu1500.http.API;
import demoxsgl_300.com.gouwu1500.http.Okhttp;
import demoxsgl_300.com.gouwu1500.utils.JSONUtils;

/**
 * 结果分析
 */
public class JieGuoFenXiActivity extends Activity {


    @BindView(R.id.fanhui)
    TextView fanhui;
    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    @BindView(R.id.tv_bingzhengvalue)
    TextView tvBingzhengvalue;
    @BindView(R.id.tv_shexiangvalue)
    TextView tvShexiangvalue;
    @BindView(R.id.imageView4)
    ImageView imageView4;
    @BindView(R.id.tv_bianzhengvalue)
    TextView tvBianzhengvalue;
    @BindView(R.id.tv_bingyinvalue)
    TextView tvBingyinvalue;
    @BindView(R.id.tv_zhifavalue)
    TextView tvZhifavalue;
    @BindView(R.id.tv_yundongvalue)
    TextView tvYundongvalue;
    @BindView(R.id.tv_yinshivalue)
    TextView tvYinshivalue;
    @BindView(R.id.next_1)
    Button next1;
    @BindView(R.id.next_2)
    Button next2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jieguofenxi);
        ButterKnife.bind(this);
        tvBingzhengvalue.setText("" + Content.Zong);
        gg();


        String fenxi = getIntent().getStringExtra("fenxi");
        if (!TextUtils.isEmpty(fenxi)) {
            ShiLiao shiLiao = JSONUtils.parseJSON(fenxi, ShiLiao.class);
            String disease_cause = shiLiao.getDisease_cause();

            String exercise_therapy = shiLiao.getExercise_therapy();
            String food_treatment = shiLiao.getFood_treatment();
            String prescription = shiLiao.getPrescription();
            String syndrome_name = shiLiao.getSyndrome_name();
            String treatment_method = shiLiao.getTreatment_method();

            tvShexiangvalue.setText("" + Content.SheTou);
            tvBianzhengvalue.setText("" + syndrome_name);
            tvBingyinvalue.setText("" + disease_cause);
            tvZhifavalue.setText("" + treatment_method);
            tvYundongvalue.setText("" + exercise_therapy);
            tvYinshivalue.setText("" + food_treatment);

            String bianzheng = "辨证" + "" + syndrome_name;
            String bingying = "病因" + "" + disease_cause;
            String zhifa = "治法" + "" + treatment_method;
            String yundong = "运动" + "" + exercise_therapy;
            String yinshi = "饮食" + "" + food_treatment;
            String bingzheng = "病症" + Content.Zong;
            String shetou = "舌像" + Content.SheTou;
            final String zong =  bingzheng + shetou+bianzheng + bingying + zhifa + yundong + yinshi ;
            //播放语音
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    textToSpeech.speak(zong,
                            TextToSpeech.QUEUE_ADD, null);
                }
            }, 1000);
            dijiao(syndrome_name, disease_cause, treatment_method, "", exercise_therapy, food_treatment);
        } else {
            String time = getIntent().getStringExtra("time");
            API.searchresult(Content.user_password, time, new Okhttp.Objectcallback() {
                @Override
                public void onsuccess(String st) {
                    Log.e("111", "xx" + st);
                    BEA bea = JSONUtils.parseJSON(st, BEA.class);

                    String exercise_therapy = bea.getExercise_therapy();
                    String food_treatment = bea.getFood_treatment();
                    String tvBianzhen = bea.getSymptom();
                    String shetou = bea.getTongue();
                    String treatment_method = bea.getTreat();
                    String Dialectical = bea.getDialectical();
                    String Pharmacy = bea.getPharmacy();

                    tvShexiangvalue.setText("" + shetou);
                    tvBianzhengvalue.setText("" + Pharmacy);
                    tvBingyinvalue.setText("" + Dialectical);
                    tvZhifavalue.setText("" + treatment_method);
                    tvYundongvalue.setText("" + exercise_therapy);
                    tvYinshivalue.setText("" + food_treatment);
                    tvBingzhengvalue.setText("" +tvBianzhen);
                }

                @Override
                public void onFalia(int code, String errst) {
                    Log.e("111", "xx" + errst);
                }
            });
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (textToSpeech.isSpeaking()) {
            textToSpeech.stop();
        } else {

        }
    }

    @OnClick({R.id.fanhui, R.id.next_1, R.id.next_2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fanhui:
                XuanZeActivity.finishActivity();
                finish();
                break;
            case R.id.next_1:
                startActivity(new Intent(this, FaKuiActivity.class));
                break;
            case R.id.next_2:
                GeRenActivity.finishActivity();
                XuanZeActivity.finishActivity();
                finish();
//                startActivity(new Intent(this, FaKuiActivity.class));
                break;
        }
    }

    TextToSpeech textToSpeech;

    public void gg() {
        //初始化TTS
        textToSpeech = new

                TextToSpeech(JieGuoFenXiActivity.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                            && result != TextToSpeech.LANG_AVAILABLE) {
                        Toast.makeText(JieGuoFenXiActivity.this, "TTS暂时不支持这种语音的朗读！",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    public void dijiao(String zhengxing, String bingyin, String zhifa, String fangyao, String yundong, String shiwu) {
        if (Content.Login == true) {
            API.insertresult(Content.user_password, Content.sex, Content.age, Content.SheTou, Content.ZhuSu,
                    Content.Zong, zhengxing, bingyin, zhifa, fangyao, yundong, shiwu, new Okhttp.Objectcallback() {
                        @Override
                        public void onsuccess(String st) {
                            Log.e("st", "xx" + st);
                            if (st.equals("success")) {
                                Toast.makeText(JieGuoFenXiActivity.this, "递交成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(JieGuoFenXiActivity.this, "递交失败", Toast.LENGTH_SHORT).show();
                            }

                        }

                        @Override
                        public void onFalia(int code, String errst) {
                            Log.e("", "xx" + errst);
                            Toast.makeText(JieGuoFenXiActivity.this, "网络错误", Toast.LENGTH_SHORT).show();

                        }
                    });

        } else {

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (textToSpeech.isSpeaking()) {
            textToSpeech.stop();
        } else {

        }
    }
}
